/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.ipti.br.tag.entity;

import java.util.Calendar;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.ipti.br.tag.util.CalendarUtil;
import org.ipti.br.tag.util.EncryptionUtil;

/**
 *
 * @author israel
 */
@Entity
public class PersonageHolidayCalendar extends AbstractEntity{

    @Column(nullable = false, length = 45)
    private String name;

    @Temporal(TemporalType.DATE)
    private Calendar initialDate;

    @Temporal(TemporalType.DATE)
    private Calendar finalDate;

    @ManyToOne
    @JoinColumn(name = "unity_id")
    private Unity unity;

    @ManyToOne
    @JoinColumn(name = "personage_id")
    private Personage personage;
    
    public PersonageHolidayCalendar() {
    }

    @Override
    public void generateHash(){
        if(getId() == null){
            String hash = "" + this.getName()
                    + ((this.getInitialDate() != null) ? CalendarUtil.getFormatedDate(this.getInitialDate()) : null)
                    + ((this.getFinalDate() != null) ? CalendarUtil.getFormatedDate(this.getFinalDate()) : null)
                    + ((this.getUnity() != null) ? this.getUnity().getId() : null)
                    + ((this.getPersonage() != null) ? this.getPersonage().getId() : null);
            this.setId(Long.valueOf(EncryptionUtil.encrypt(hash).hashCode()));
        }
    }

    public PersonageHolidayCalendar(String name, Calendar initialDate, Calendar finalDate, Unity unity, Personage personage) {
        this.name = name;
        this.initialDate = initialDate;
        this.finalDate = finalDate;
        this.unity = unity;
        this.personage = personage;
    }

    public Personage getPersonage() {
        return personage;
    }

    public void setPersonage(Personage personage) {
        this.personage = personage;
    }

    public Calendar getFinalDate() {
        return finalDate;
    }

    public void setFinalDate(Calendar finalDate) {
        this.finalDate = finalDate;
    }

    public Calendar getInitialDate() {
        return initialDate;
    }

    public void setInitialDate(Calendar initialDate) {
        this.initialDate = initialDate;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Unity getUnity() {
        return unity;
    }

    public void setUnity(Unity unity) {
        this.unity = unity;
    }
}
